home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / sw / explode.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.9 KB  |  162 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stream.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "explode.h"
  21. #include <gl/gl.h>
  22. extern "C" {
  23. #include "image.h"
  24. int        iclose(IMAGE*);
  25. int        getrow(IMAGE*, unsigned short*, unsigned int, unsigned int);
  26. }
  27.  
  28. #define    EXPLOSIONFRAMES    30
  29. #define    FADEFRAME    23
  30. #define    FADINGFRAMES    (EXPLOSIONFRAMES - FADEFRAME - 1)
  31.  
  32. static unsigned long*    explosions[EXPLOSIONFRAMES][7];
  33. static unsigned short    rbuf[256], gbuf[256], bbuf[256];
  34.  
  35. //IMAGE *iopen(char *file, char *mode, ...);
  36. IMAGE *iopen(char *file, char *mode);
  37.  
  38. static unsigned long*    readImage(char* fname, float opacity)
  39. {
  40.   float intensity;
  41.   IMAGE* image;
  42.   short x, y;
  43.   unsigned long* i, *s;
  44.  
  45.   if ((image = iopen(fname, "r")) == NULL) {
  46.     cerr << "can't open image " << fname << endl;
  47.     return NULL;                // can't open image
  48.   }
  49.   if (image->xsize != 64 || image->ysize != 64) {    // wrong size
  50.     cerr << "bad image size: " << image->xsize << "x" << image->ysize << "\n";
  51.     return NULL;
  52.   }
  53.   if (image->zsize < 3) {            // not an RGB image
  54.     cerr << "not an RGB image\n";
  55.     return NULL;
  56.   }
  57.  
  58.   i = (unsigned long*)malloc(image->xsize*image->ysize*sizeof(unsigned long));
  59.   if (!i) {
  60.     cerr << "can't allocate memory\n";
  61.     return NULL;
  62.   }
  63.   for (s = i, y = 0; y < image->ysize; y++) {
  64.     getrow(image, rbuf, y, 0);
  65.     getrow(image, gbuf, y, 1);
  66.     getrow(image, bbuf, y, 2);
  67.     for (x = 0; x < image->xsize; s++, x++) {
  68.       intensity = float((rbuf[x] > gbuf[x]) ? rbuf[x] : gbuf[x]);
  69.       if (float(bbuf[x]) > intensity) intensity = float(bbuf[x]);
  70.       intensity *= 2.5 * opacity;
  71.       if (intensity > 255.0) intensity = 255.0;
  72.       *s = ((unsigned long)(intensity + 0.5) << 24) |
  73.         ((unsigned long)(bbuf[x] * opacity + 0.5) << 16) |
  74.         ((unsigned long)(gbuf[x] * opacity + 0.5) << 8) |
  75.         (unsigned long)(rbuf[x] * opacity + 0.5);
  76.     }
  77.   }
  78.   iclose(image);
  79.   return i;
  80. }
  81.  
  82. unsigned long*        shrinkImage(unsigned long* image, int size)
  83. {
  84.   unsigned long* shrunk = (unsigned long*)malloc((size>>1) * (size>>1) *
  85.                             sizeof(unsigned long));
  86.   if (!shrunk) return NULL;
  87.   unsigned long* s1 = image, *s2 = shrunk;
  88.   for (int i = 0; i < size; s1 += size, i += 2)
  89.     for (int j = 0; j < size; s1 += 2, s2++, j += 2) {
  90.       // FIXME -- average four pixels instead of ZOH
  91.       *s2 = *s1;
  92.     }
  93.   return shrunk;
  94. }
  95.  
  96. int            readExplosion(const char* dir)
  97. {
  98.   char namebuf[256];
  99.   for (int i = 0; i < EXPLOSIONFRAMES; i++) {
  100.     float opacity = (i < FADEFRAME) ? 1.0 :
  101.                 float(EXPLOSIONFRAMES-i-1) / FADINGFRAMES;
  102.     sprintf(namebuf, "%s/e%02d.rgb", dir, i+1);
  103.     if (!(explosions[i][0] = readImage(namebuf, opacity)))
  104.       return FALSE;
  105.     for (int j = 1, s = 64; j < 7; s >>= 1, j++)
  106.       if (!(explosions[i][j] = shrinkImage(explosions[i][j-1], s))) {
  107.     cerr << "couldn't shrink to size " << (s >> 1) << endl;
  108.     return FALSE;
  109.       }
  110.   }
  111.   return TRUE;
  112. }
  113.  
  114. static short        erv[4][2];
  115.  
  116. void            drawExplosion(short x, short y, short r, float z,
  117.                                 float t)
  118. {
  119.   if (r <= 0) return;                // too small
  120.  
  121.   short size, sizeIndex, zoom = 1;
  122.   if (r > 32) {
  123.     while (r > 64) { zoom++; r -= 64; }
  124.     sizeIndex = 0;
  125.   }
  126.   else if (r > 16) sizeIndex = 1;
  127.   else if (r > 8) sizeIndex = 2;
  128.   else if (r > 4) sizeIndex = 3;
  129.   else if (r > 2) sizeIndex = 4;
  130.   else if (r > 1) sizeIndex = 5;
  131.   else sizeIndex = 6;
  132.   short isize = 128 >> (sizeIndex + 1);
  133.   size = zoom * isize;
  134.  
  135.   int frame = int((1.0 - t) * (EXPLOSIONFRAMES - 1) + 0.5);
  136.   if (frame >= EXPLOSIONFRAMES) frame = EXPLOSIONFRAMES - 1;
  137.   x -= (size >> 1);
  138.   y -= (size >> 1);
  139.  
  140.   erv[0][0] = x; erv[0][1] = y;
  141.   erv[1][0] = x+size-1; erv[1][1] = y;
  142.   erv[2][0] = x+size-1; erv[2][1] = y+size-1;
  143.   erv[3][0] = x; erv[3][1] = y+size-1;
  144.   wmpack(0);
  145.   stencil(TRUE, 1, SF_ALWAYS, 0x1, ST_KEEP, ST_KEEP, ST_REPLACE);
  146.   pushmatrix();
  147.     translate(0.0, 0.0, -z);
  148.     bgnpolygon();
  149.       v2s(erv[0]);
  150.       v2s(erv[1]);
  151.       v2s(erv[2]);
  152.       v2s(erv[3]);
  153.     endpolygon();
  154.   popmatrix();
  155.   wmpack(0xffffffff);
  156.   stencil(TRUE, 1, SF_EQUAL, 0x1, ST_ZERO, ST_ZERO, ST_ZERO);
  157.   if (zoom != 0) rectzoom(float(zoom), float(zoom));
  158.   lrectwrite(x, y, x + isize - 1, y + isize - 1, explosions[frame][sizeIndex]);
  159.   if (zoom != 0) rectzoom(1.0, 1.0);
  160.   stencil(FALSE, 0, SF_ALWAYS, 0, ST_KEEP, ST_KEEP, ST_KEEP);
  161. }
  162.